#include"utils.h"

QMap<int,QString> Utils::readMap(QString filePath)
{
    QMap<int,QString> map;
    QFile f(filePath);
    f.open(QIODevice::ReadOnly);
    QDataStream in(&f);
    int id;
    char* temp;
    while(!f.atEnd())
    {
        in>>id;
        in>>temp;
        map.insert(id,QString::fromUtf8(temp));
        delete[] temp;
    }
    f.close();
    return map;
}

void Utils::writeMap(QString filePath,QMap<int,QString>& map)
{
    QFile f(filePath);
    f.open(QIODevice::WriteOnly);
    QDataStream out(&f);

    QMapIterator<int,QString> it(map);
    while(it.hasNext())
    {
        it.next();
        out<<it.key();
        out<<it.value().toUtf8().data();
    }
    f.close();
}

void Utils::updateSeq(QString dir,QTreeWidgetItem* parent)
{
    QDir mdir(dir);

    QFile f(mdir.absolutePath()+"/seq");
    f.open(QIODevice::WriteOnly);
    QDataStream out(&f);

    int count = parent->childCount();

    for(int i=0;i<count;i++)
    {
        QString s = parent->child(i)->text(0);
        out<<s.toUtf8().data();
    }

    f.close();
}

QStringList Utils::initialSeq(QString dir,int type)
{
    QDir mdir(dir);
    QStringList strlist;

    switch(type)
    {
    case CITY_ITEM_TYPE:
        strlist = mdir.entryList(QDir::Dirs|QDir::NoDotAndDotDot);
        break;
    case EVENT_ITEM_TYPE:
        strlist = mdir.entryList(QDir::Dirs|QDir::NoDotAndDotDot);
        break;
    case MISSION_ITEM_TYPE:
        strlist = mdir.entryList(QStringList()<<QString(MISSION_FILE_SUFFIX));
        break;
    }

    QFile f(mdir.absolutePath()+"/seq");
    f.open(QIODevice::WriteOnly);
    QDataStream out(&f);

    int count = strlist.length();

    for(int i=0;i<count;i++)
    {
        QString s = strlist.at(i);
        out<<s.toUtf8().data();
    }
    f.close();

    return strlist;
}


QStringList Utils::getSeq(QString fileName)
{
    QStringList result;
    QFile f(fileName);
    if(f.exists())
    {
        f.open(QIODevice::ReadOnly);
        QDataStream in(&f);
        char* tmp;
        while(!f.atEnd())
        {
            in>>tmp;
            result<<QString::fromUtf8(tmp);
            delete[] tmp;
        }
        f.close();
    }

    return result;
}

void Utils::writeSeq(QString fileName,QStringList strlist)
{
    QFile f(fileName);
    f.open(QIODevice::WriteOnly);
    QDataStream out(&f);
    for(int i=0;i<strlist.length();i++)
        out<<strlist.at(i).toUtf8().data();
    f.close();
}

void Utils::deleteFile(QString dirPath,QString fileName)
{
    QDir mdir(dirPath);
    mdir.remove(fileName);
}

void Utils::expandTreeItem(QTreeWidgetItem* item)
{
    item->setExpanded(true);
    int cnt = item->childCount();
    if(cnt>0)
    {
        for(int i=0;i<cnt;i++)
            expandTreeItem(item->child(i));
    }
}

void Utils::packupTreeItem(QTreeWidgetItem* item)
{
    item->setExpanded(false);
    int cnt = item->childCount();
    if(cnt>0)
    {
        for(int i=0;i<cnt;i++)
            expandTreeItem(item->child(i));
    }
}
